package com.godtree.util.bazhuayu;

import java.util.Objects;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

import static com.godtree.util.bazhuayu.TokenTools.getToken;

/**
 * @author Lisicheng
 * @date 2020-6-4
 */
@SuppressWarnings("all")
public final class UpdateTools {

  private static final String URL = "https://dataapi.bazhuayu.com/api/notexportdata/update?taskId={}";

  private static final String REMOVE_URL = "https://dataapi.bazhuayu.com/api/task/RemoveDataByTaskId?taskId={}";

  /**
   * 标记任务数据为已导出状态
   * <p>
   * 实例数据 { "error": "success", "error_Description": "操作成功" }
   *
   * @param taskId 任务id
   * @param token  token
   * @return 标记成功 true，false
   */
  public static boolean update(String taskId, String token) {
    JSONObject json = JSONObject.parseObject(HttpUtil.createPost(StrUtil.format(URL, taskId))
        .header("Authorization", getToken())
        .execute()
        .body());
    return Objects.equals(Constant.MSG, json.getString(Constant.MSG_ERROR));
  }

  /**
   * 清除所有数据
   *
   * @param taskId 任务id
   * @param token  token
   * @return 标记成功 true，false
   */
  public static boolean remove(String taskId, String token) {
    JSONObject json = JSONObject.parseObject(HttpUtil.createPost(StrUtil.format(REMOVE_URL, taskId))
        .header("Authorization", getToken())
        .execute()
        .body());
    return Objects.equals(Constant.MSG, json.getString(Constant.MSG_ERROR));
  }
}
